<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="代码" prop="CCE">
        <el-input
          v-model="queryParams.CCE"
          placeholder="请输入代码"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="名称" prop="DES">
        <el-input
          v-model="queryParams.DES"
          placeholder="请输入名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="类型" prop="YLX">
        <el-select v-model="queryParams.YLX" placeholder="请选择类型" clearable>
          <el-option
            v-for="dict in dict.type.x3_1002"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="直属办事处" prop="YZSBSC">
        <el-select v-model="queryParams.YZSBSC" placeholder="请选择直属办事处" clearable>
          <el-option
            v-for="dict in dict.type.x3_1"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="状态" prop="YSTA">
        <el-select v-model="queryParams.YSTA" placeholder="请选择状态" clearable>
          <el-option
            v-for="dict in dict.type.x3_1003"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="备注" prop="YBZ">
        <el-input
          v-model="queryParams.YBZ"
          placeholder="请输入备注"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['x3base:gescce:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['x3base:gescce:remove']"
        >删除</el-button>
      </el-col>

      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="gescceList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="代码" align="center" prop="CCE" />
      <el-table-column label="名称" align="center" prop="DES" />
      <el-table-column label="一级办事处" align="center" prop="YSJBM" />
      <el-table-column label="一级办事处名称" align="center" prop="C13" />
      <el-table-column label="类型" align="center" prop="YLX">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.x3_1002" :value="scope.row.YLX"/>
        </template>
      </el-table-column>
      <el-table-column label="直属办事处" align="center" prop="YZSBSC">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.x3_1" :value="scope.row.YZSBSC"/>
        </template>
      </el-table-column>
      <el-table-column label="状态" align="center" prop="YSTA">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.x3_1003" :value="scope.row.YSTA"/>
        </template>
      </el-table-column>
      <el-table-column label="备注" align="center" prop="YBZ" />
      <el-table-column label="创建日期" align="center" prop="CREDAT" width="180">
        <template slot-scope="scope">
          <span>{{ scope.row.CREDAT}}</span>
        </template>
      </el-table-column>
      <el-table-column label="审核日期" align="center" prop="YSHDATA" width="180">
        <template slot-scope="scope">
          <span>{{ scope.row.YSHDATA}}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)"
                     v-hasPermi="['x3base:gescce:edit']" v-if="scope.row.YSTA<=1">修改</el-button>
          <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)"
                     v-hasPermi="['x3base:gescce:remove']" v-if="scope.row.YSTA<=1">删除</el-button>
          <el-button size="mini" type="text" @click="handle('a',scope.row)" v-if="scope.row.YSTA==1">审核</el-button>
          <el-button size="mini" type="text" @click="handle('b',scope.row)" v-if="scope.row.YSTA==2">停用</el-button>
          <el-button size="mini" type="text" @click="handle('c',scope.row)" v-if="scope.row.YSTA==2">驳回</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改二级办事处对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="一级办事处" prop="YSJBM">
          <el-select v-model="form.YSJBM" placeholder="请选择一级办事处">
            <el-option
              v-for="item in YSJBMOptions"
              :key="item.CCE"
              :label="item.DES"
              :value="item.CCE"
              :disabled="item.YSTA == 3"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="代码" prop="CCE">
          <el-input v-model="form.CCE" placeholder="请输入代码" />
        </el-form-item>
        <el-form-item label="名称" prop="DES">
          <el-input v-model="form.DES" placeholder="请输入名称" />
        </el-form-item>
        <el-form-item label="类型" prop="YLX">
          <el-select v-model="form.YLX" placeholder="请选择类型">
            <el-option
              v-for="dict in dict.type.x3_1002"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="直属办事处" prop="YZSBSC">
          <el-select v-model="form.YZSBSC" placeholder="请选择直属办事处">
            <el-option
              v-for="dict in dict.type.x3_1"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="状态" prop="YSTA">
          <el-select v-model="form.YSTA" placeholder="请选择状态">
            <el-option
              v-for="dict in dict.type.x3_1003"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="备注" prop="YBZ">
          <el-input v-model="form.YBZ" placeholder="请输入备注" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listGescce, getGescce, delGescce, addGescce, updateGescce ,actionGescce} from "@/api/x3base/gescce2";
import * as cce1 from "@/api/x3base/gescce";
export default {
  name: "Gescce2",
  dicts: ['x3_1', 'x3_1002', 'x3_1003'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 二级办事处表格数据
      gescceList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 999,
        CCE: null,
        DES: null,
        YLX: null,
        YZSBSC: null,
        YSTA: null,
        YBZ: null,
        CREDAT: null,
        YSHDATA: null,
        YSJBM: null,
        ZYSJBM: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      },
      isAdd:false,
      YSJBMOptions:[],//上级办事处
    };
  },
  created() {
    this.getList();
    this.getYSJBMList();
  },
  methods: {
    /** 查询二级办事处列表 */
    getList() {
      this.loading = true;
      listGescce(this.queryParams).then(response => {
        this.gescceList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    /** 查询二级办事处列表 */
    getYSJBMList() {
      cce1.listGescce({pageSize: 999}).then(response => {
        this.YSJBMOptions = response.rows;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        CCE: null,
        DES: null,
        YLX: null,
        YZSBSC: null,
        YSTA: null,
        YBZ: null,
        CREDAT: null,
        YSHDATA: null,
        YSJBM: null,
        ZYSJBM: null,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.CCE)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加二级办事处";
      this.isAdd=true
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const CCE = row.CCE || this.ids
      this.isAdd=false
      getGescce(CCE).then(response => {
        this.form = response.data["HEAD"];
        console.log(this.form)
        this.open = true;
        this.title = "修改二级办事处";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.isAdd != true) {
            updateGescce(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            }).catch(error=>{
              this.$modal.msgError(error)
            });
          } else {
            addGescce(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            }).catch(error=>{
              this.$modal.msgError(error)
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const CCEs = row.CCE || this.ids;
      this.$modal.confirm('是否确认删除二级办事处编号为"' + CCEs + '"的数据项？').then(function() {
        return delGescce(CCEs);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('x3base/gescce/export', {
        ...this.queryParams
      }, `gescce_${new Date().getTime()}.xlsx`)
    },
    /** 修改按钮操作 */
    handle(flag,row) {
      actionGescce(row,flag).then(response => {
        this.$modal.msgSuccess("操作成功");
        this.open = false;
        this.getList();
      });
    },
  }
};
</script>
